package component

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commConfig"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/FileName"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Reflect"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEnv"
	"io/ioutil"
)

type manageConfigService struct {
}

func (e *manageConfigService) InitializeByFilePath(configName string, fileName string) error {
	filePath := FileName.GetOsFilePath(commEnv.Service.WorkDir + "/conf/" + commEnv.Service.ServiceType + "/" + commEnv.Service.ServiceName + "/" + fileName)

	jsn, err := ioutil.ReadFile(filePath)
	if err != nil {
		return err
	}

	serverConfig := Json.BuildMapByJson(string(jsn))

	// 取出配置内容
	remark := serverConfig["remark"]
	configValue := serverConfig["configValue"]
	configParam := serverConfig["configParam"]
	if Method.HasNull(remark, configValue, configParam) {
		return errors.New("初始化配置参数格式不能为空，remark, configValue, configParam")
	}

	// 检查：数据格式是否合法
	if !Reflect.IsString(remark) || !Reflect.IsMap(configValue) || !Reflect.IsMap(configParam) {
		return errors.New("初始化配置参数格式不能为空，remark, configValue, configParam")
	}

	configEntity := commRedisService.GetConfigEntity(edgeEnv.Name.GetManagerName(), commEnv.Service.ServiceType, configName)
	if configEntity == nil {
		newConfig := edgeEntity.Type.ConfigEntity.Instance()
		newConfig.ServiceType = commEnv.Service.ServiceType
		newConfig.ServiceName = edgeEnv.Name.GetManagerName()
		newConfig.ConfigName = configName
		newConfig.Remark = remark.(string)
		Map.Append(newConfig.ConfigValue, configValue.(map[string]interface{}))
		Map.Append(newConfig.ConfigParam, configParam.(map[string]interface{}))

		commEntityManager.InsertEntity(newConfig)
	}

	// 在通用配置这边，也形成一份后期通告的配置
	commConfig.Initialize(configName, string(jsn))

	return nil
}

func (e *manageConfigService) GetConfigValueThis(configName string) (map[string]interface{}, error) {
	return e.GetConfigValue(edgeEnv.Name.GetManagerName(), commEnv.Service.ServiceType, configName)
}

func (e *manageConfigService) GetConfigValue(serviceName, serviceType, configName string) (map[string]interface{}, error) {
	// 获得配置信息
	configEntity := commRedisService.GetConfigEntity(serviceName, serviceType, configName)
	if configEntity == nil {
		configEntity = edgeEntity.Type.ConfigEntity.Instance()
		configEntity.ServiceName = serviceName
		configEntity.ServiceType = serviceType
		configEntity.ConfigName = configName

		err := commEntityManager.InsertEntity(configEntity)
		if err != nil {
			return nil, err
		}
	}

	return configEntity.ConfigValue, nil
}

func (e *manageConfigService) SaveConfigValueThis(configName string, configValue map[string]interface{}) error {
	return e.SaveConfigValue(edgeEnv.Name.GetManagerName(), commEnv.Service.ServiceType, configName, configValue)
}

func (e *manageConfigService) SaveConfigValue(serviceName, serviceType, configName string, configValue map[string]interface{}) error {
	if configValue == nil {
		return errors.New("输入参数不能为空")
	}

	configEntity := commRedisService.GetConfigEntity(serviceName, serviceType, configName)
	if configEntity == nil {
		configEntity = edgeEntity.Type.ConfigEntity.Instance()
		configEntity.ServiceName = serviceName
		configEntity.ServiceType = serviceType
		configEntity.ConfigName = configName
		configEntity.ConfigValue = configValue

		err := commEntityManager.InsertEntity(configEntity)
		if err != nil {
			return err
		}
	}

	configEntity.ConfigValue = configValue
	return commEntityManager.UpdateEntity(configEntity)
}
